package com.cimp.service.iqy.interfaces;

import com.cimp.domain.iqy.ResultBean;
import com.cimp.util.fuse.HttpUtil;
import com.cimp.util.iqy.CommonUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * [简要描述]: 爱奇艺数据接口
 * [详细描述]:
 *
 * @author huojinhao
 * @date 2018/7/4
 */
@Service
public class DataInterface {
    
    private Logger logger = Logger.getLogger(DataInterface.class);
    
    /**
     * 获取全量ID列表
     */
    public ResultBean<Long> queryChannelSnapshot(String url, int chnId, int type) {
        Map<String, String> params = new HashMap<String, String>();
        String token = CommonUtil.generateToken();
        params.put("token", token);
        params.put("chnId", String.valueOf(chnId));
        params.put("type", String.valueOf(type));
        String res = HttpUtil.doGet(url, params);
        try{
            JSONObject jsonObject = JSONObject.fromObject(res);
            ResultBean<Long> resultBean = new ResultBean<Long>();
            String code = jsonObject.getString("code");
            if(jsonObject.has("data")){
                JSONArray jsonArray = jsonObject.getJSONArray("data");
                List<Long> list = new ArrayList<Long>();
                for(int i=0;i<jsonArray.size();i++){
                    Long l = jsonArray.getLong(i);
                    list.add(l);
                }
                resultBean.setData(list);
            }
            resultBean.setCode(code);
            return resultBean;
        }catch (Exception e){
            logger.error("queryChannelSnapshot Json parse error , the response json is:" + res, e);
        }
        return null;
    }

    /**
     * 获取增量ID列表
     */
    public ResultBean<Long> queryContentChange(String url,int startTs,int endTs,int type, int pn,long lastId) {
        Map<String, String> params = new HashMap<String, String>();
        String token = CommonUtil.generateToken();
        params.put("token", token);
        params.put("startTs", String.valueOf(startTs));
        params.put("endTs", String.valueOf(endTs));
        params.put("type", String.valueOf(type));
        params.put("pn", String.valueOf(pn));
        params.put("lastId", String.valueOf(lastId));
        String res = HttpUtil.doGet(url , params);

        try{
            JSONObject jsonObject = JSONObject.fromObject(res);
            ResultBean<Long> resultBean = new ResultBean<Long>();
            resultBean.setCode(jsonObject.getString("code"));
            if(jsonObject.has("data")){
                JSONArray jsonArray = jsonObject.getJSONArray("data");
                List<Long> list = new ArrayList<Long>();
                for(int i=0;i<jsonArray.size();i++){
                    Long l = jsonArray.getLong(i);
                    list.add(l);
                }
                resultBean.setData(list);
            }
            if(jsonObject.has("total")){
                resultBean.setTotal(jsonObject.getInt("total"));
            }
            if(jsonObject.has("ps")){
                resultBean.setPs(jsonObject.getInt("ps"));
            }
            return resultBean;
        }catch (Exception e){
            logger.error("queryContentChange Json parse error , the response json is:" + res, e);
        }
        return null;
    }

    /**
     * 查询人物全量ID列表
     */
    public ResultBean<Long> queryPeopleSnapshot(String url){
        Map<String, String> params = new HashMap<String, String>();
        String token = CommonUtil.generateToken();
        params.put("token", token);
        String res = HttpUtil.doGet(url, params);
        try{
            JSONObject jsonObject = JSONObject.fromObject(res);
            ResultBean<Long> resultBean = new ResultBean<Long>();
            resultBean.setCode(jsonObject.getString("code"));
            if(jsonObject.has("data")){
                JSONArray jsonArray = jsonObject.getJSONArray("data");
                List<Long> list = new ArrayList<Long>();
                for(int i=0;i<jsonArray.size();i++){
                    Long l = jsonArray.getLong(i);
                    list.add(l);
                }
                resultBean.setData(list);
            }
            return resultBean;
        }catch (Exception e){
            logger.error("queryPeopleSnapshot Json parse error , the response json is:" + res, e);
        }
        return null;
    }

    /**
     * 某时间段内变更的人物 ID 列表
     */
    public ResultBean<Long> queryPeopleChange(String url,int startTs,int endTs, int pn,long lastId) {
        Map<String, String> params = new HashMap<String, String>();
        String token = CommonUtil.generateToken();
        params.put("token", token);
        params.put("startTs", String.valueOf(startTs));
        params.put("endTs", String.valueOf(endTs));
        params.put("pn", String.valueOf(pn));
        params.put("lastId", String.valueOf(lastId));
        String res = HttpUtil.doGet(url , params);
        try{
            JSONObject jsonObject = JSONObject.fromObject(res);
            ResultBean<Long> resultBean = new ResultBean<Long>();
            resultBean.setCode(jsonObject.getString("code"));
            if(jsonObject.has("data")){
                JSONArray jsonArray = jsonObject.getJSONArray("data");
                List<Long> list = new ArrayList<Long>();
                for (int i = 0; i < jsonArray.size(); i++) {
                    Long l = jsonArray.getLong(i);
                    list.add(l);
                }
                resultBean.setData(list);
            }
            if(jsonObject.has("hasNext")){
                resultBean.setHasNext(jsonObject.getInt("hasNext"));
            }
            if(jsonObject.has("ps")){
                resultBean.setPs(jsonObject.getInt("ps"));
            }
            return resultBean;
        }catch (Exception e){
            logger.error("queryPeopleChange Json parse error , the response json is:" + res, e);
        }
        return null;
    }
}
